package com.example.red_tea_house.moudle.LoginAndRegister.controller;

import android.content.Context;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.annotation.UrlType;
import com.example.red_tea_house.entity.LoginBean;
import com.example.red_tea_house.moudle.LoginAndRegister.model.LegalData;
import com.example.red_tea_house.moudle.LoginAndRegister.view.LoginActivity;
import com.example.red_tea_house.util.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class LoginController {
    private final LoginActivity loginActivity;
    private final LegalData legalData=new LegalData();

    private final String loginVertifyUrl="login";
    private String loginReturnCode;
    private final String loginSucess="1";
    private final String NickNameInd="NN:";
    private final String AutographInd="AG:";
    Context context= MyApplication.getContext();
    public LoginController(LoginActivity loginActivity){
        this.loginActivity=loginActivity;
    }
    public void loginJudge(LoginBean loginBean){
        if(legalData.isLegalData(loginBean)) {
            OkHttpUtils
                    .get()
                    .url(UrlType.REMOTESERVER_URL +loginVertifyUrl)
                    .addParams("phone",loginBean.getPhone())
                    .addParams("password",loginBean.getPassword())
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            /*
                             * TODO:处理报错信息
                             */
                        }
                        @Override
                        public void onResponse(String response, int id) {
                            loginReturnCode =response.substring(1,2);
                            if (loginReturnCode.equals(loginSucess)) {
                                response.indexOf(NickNameInd);
                                loginActivity.returnLoginStateMessage(response.substring(response.indexOf(NickNameInd)+3,response.indexOf(AutographInd)),response.substring(response.indexOf(AutographInd)+3));
                            } else {
                                ToastUtil.showToast(context, "密码错误");
                            }
                        }
                    });
        }
    }
}
